/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.auction.service.mapper;

import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.auction.domain.AuctionRecord;
import co.yixiang.modules.auction.service.dto.AuctionRecordDto;
import co.yixiang.modules.auction.service.dto.AuctionRecordQueryCriteria;
import co.yixiang.modules.auction.service.dto.SingleDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
* @author ycl
* @date 2021-02-18
*/
@Repository
public interface AuctionRecordMapper extends CoreMapper<AuctionRecord> {

    @Select("SELECT id , title from yx_auction_product where type = 1 and id not in (SELECT auction_product_id from yx_auction_record)")
    List<Map<String, Object>> getAuctionProduct();


    @Select(" <script>  select ar.*,ap.title as productName, yacf.title as configName , yu.phone as phone , yau.real_name as name ," +
            " yu2.phone as buyPhone , ya2.real_name as buyName , " +
            " (select u.nickname from  yx_auction_record c left join yx_user u on c.user_id=u.uid " +
            " where c.auction_product_id=ar.auction_product_id order by c.price desc limit 1) as nickname, " +
            " (select max(ar1.price) from yx_auction_record ar1 where ar1.parent_id=ar.id or ar1.id=ar.id) as nowPrice " +
            " from yx_auction_record ar left join yx_auction_product ap on ar.auction_product_id=ap.id " +
            " LEFT JOIN yx_auction_config yacf on ar.time_id = yacf.id LEFT JOIN yx_authentication yau on yau.uid = ar.user_id " +
            " LEFT JOIN yx_user yu on yu.uid = ar.user_id LEFT JOIN yx_auction_order yao on ar.id = yao.auction_record_id and yao.status != 5 " +
            " LEFT JOIN yx_user yu2 on yao.user_id = yu2.uid LEFT JOIN yx_authentication ya2 on yao.user_id = ya2.uid" +
            "  where 1 = 1 " +
            " <if test=\"parentId == null \"> and (ar.parent_id=0 or ar.parent_id is null) </if> " +
            " <if test=\"parentId != null \"> and ar.parent_id=#{parentId} </if>" +
            " <if test=\"productName != null and productName != ''\"> and ap.title  like CONCAT('%',#{productName},'%') </if> " +
            " <if test=\"phone != null and phone != ''\"> and yu.phone like CONCAT('%',#{phone},'%') </if> " +
            " <if test=\"buyPhone != null and buyPhone != ''\"> and yu2.phone like CONCAT('%',#{buyPhone},'%') </if> " +
            " <if test=\"name != null and name != ''\"> and yau.real_name like CONCAT('%',#{name},'%') </if> " +
            " <if test=\"buyName != null and buyName != ''\"> and ya2.real_name like CONCAT('%',#{buyName},'%') </if> " +
            " <if test=\"id != null and id != ''\"> and ar.id = #{id} </if> " +
            " order by ar.update_time desc </script> ")
    List<AuctionRecordDto> selectAuctionRecordList(AuctionRecordQueryCriteria criteria);


    @Select(" <script> SELECT yar.id , yar.price , yar.start_time , yar.is_buy , yar.`status` , yar.confirm_voucher , yar.pay_type , yap.title from yx_auction_record yar , yx_auction_product yap where \n" +
            " yar.auction_product_id = yap.id AND TO_DAYS(yar.start_time) = TO_DAYS(now())" +
            " <if test=\"productName != null and productName != ''\"> and yap.title  like CONCAT('%',#{productName},'%') </if>  </script> ")
    List<SingleDto> singleList(@Param("productName") String productName);

}
